package com.lsp.microuser.point.service.impl;

import com.lsp.microcommon.entity.Point;
import com.lsp.microcommon.entity.PointRetry;
import com.lsp.microuser.point.dao.PointRetryDao;
import com.lsp.microuser.point.service.PointRetryService;
import com.lsp.microuser.point.service.PointService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PointRetryServiceImpl implements PointRetryService {

    @Resource
    private PointRetryDao pointRetryDao;

    @Resource
    private PointService pointService;

    @Override
    public void addPointRetryInfo(PointRetry pointRetry) {
        pointRetryDao.insert(pointRetry);
    }

    @Override
    public void pointRetry() {

        List<PointRetry> pointRetries = pointRetryDao.pointRetryList();
        for (PointRetry p: pointRetries) {
            Point point = new Point();
            BeanUtils.copyProperties(p,point);
            point.setUpdateTime(null);
            boolean flag = false;
//            如果使用线程池执行如下逻辑有什么优点或者缺点
            do {
                try {
                    int a = 10/0;
                    pointService.addPoint(point);
                    pointRetryDao.deleteById(p.getId());
                    flag = true;
                } catch (Exception e) {
                    e.printStackTrace();
                    p.setRetryNumber(p.getRetryNumber() + 1);
                    pointRetryDao.update(p);
                    if(p.getRetryNumber() >= 3){
                        flag = true;
//                        break;
                    }
                }
            }while(!flag);
        }
    }

}
